package com.ady.webmgic.util;/**
 * Created by pc on 2017/11/24 0024.
 */

import com.ady.util.FrameUtil;
import com.ady.webmgic.ProxyProviderFactory;
import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import us.codecraft.webmagic.*;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.proxy.ProxyProvider;

import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：webmagic工具类 <br>
 * <b>time</b>：2017-11-24 10:17 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class WebMagicUtil {
    private static Logger logger = Logger.getLogger(WebMagicUtil.class);

    /**
     * 获取响应的contenttype
     *
     * @param page
     * @return
     */
    public static String getContentType(Page page) {
        if (page != null && page.getHeaders() != null) {
            final List<String> list = page.getHeaders().get("Content-Type");
            if (list != null && list.size() != 0) {
                return list.get(0);
            }
        }
        return null;
    }

    /**
     * 获取cookie
     *
     * @param page
     * @return
     */
    public static Map<String, String> getCookie(Page page) {
        Map<String, String> cookieMap = FrameUtil.newHashMap();
        if (page != null && page.getHeaders() != null) {
            Map<String, List<String>> headers = page.getHeaders();
            final List<String> list = headers.get("Set-Cookie");
            for (String s : list) {
                int i = s.indexOf(";");
                if (i != -1) {
                    s = s.substring(0, i);
                    int keyValueIndex = s.indexOf("=");
                    String cookieName = s.substring(0, keyValueIndex);
                    String cookieValue = s.substring(keyValueIndex + 1);
                    cookieMap.put(cookieName, cookieValue);

                }
            }
        }
        return cookieMap;
    }

    //抓取的超时时间，默认10秒
    public static int SITE_TIME_OUT = 10000;
    //默认失败重试次数，默认5尺
    public static int SITE_CYCLE_RETRY_TIMES = 5;
    //抓取任务之间的休眠时间
    public static int SITE_SLEEP_TIME = 1000;

    public static Site me() {
        return Site.me().setTimeOut(SITE_TIME_OUT).setCycleRetryTimes(SITE_CYCLE_RETRY_TIMES).setSleepTime(SITE_SLEEP_TIME);
    }

    public static Spider create(PageProcessor pageProcessor) {
        Downloader downloader = defaultDownloader();
        return Spider.create(pageProcessor).setSpiderListeners(FrameUtil.newArrayList(new SpiderListener() {
            @Override
            public void onSuccess(Request request) {
                logger.info("collect success:" + request.getUrl());
            }

            @Override
            public void onError(Request request) {
                logger.error("collect error:" + request.getUrl());
            }
        })).setDownloader(downloader);
    }

    public static Downloader defaultDownloader() {
        if (isUseProxy()) {
            HttpClientDownloader downloader = new HttpClientDownloader();
            ProxyProvider proxyProvider = ProxyProviderFactory.createProxyProvider();
            downloader.setProxyProvider(proxyProvider);
            return downloader;
        }
        return null;
    }

    /**
     * 是否使用代理
     *
     * @return
     */
    public static boolean isUseProxy() {
        return false;
    }

    /**
     * 判断page是否成功
     *
     * @param page
     * @return
     */
    public static boolean isSuccess(Page page) {
        return page != null && page.isDownloadSuccess() && page.getStatusCode() != HttpStatus.SC_NOT_FOUND;
    }

    public static void main(String[] args) {
        System.out.println(isUseProxy());
    }

}
